package extract;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

import model.Biologist;
import model.Family;
import model.Genus;
import model.Specie;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import control.CConnection;
import control.CFamily;
import control.CGenus;
import control.CSpecies;

public class ExtractFamily 
{
	
	public static void main(String args[])
	{
		CConnection cn = new CConnection();
		
		Biologist biolgist = new Biologist();
		biolgist.setPass_number("1631230");
		
		Document doc;
		try 
		{
			cn.createConnection();
			CFamily cFamily = new CFamily(cn);
			CGenus cGenus = new CGenus(cn);
			CSpecies cSpecie = new CSpecies(cn);
			
			doc = Jsoup.connect("http://reptile-database.reptarium.cz/advanced_search?taxon=snake&location=Brazil&submit=Search").timeout(60000).get();
			Elements species = doc.select("#content ul li a");
			
			for (Element specieElement : species) 
			{
				String linkHref = specieElement.attr("href");
				Document snakePage = Jsoup.connect("http://reptile-database.reptarium.cz"+linkHref).timeout(60000).get();
				Elements data = snakePage.select("table.species td");
				
				Family family = new Family();
				family.setName(data.get(1).text().split(",")[0]);
				family.setDescription(data.get(1).text().split(",")[0]);
				
				try
				{
					cFamily.create(family);
				}
				catch (SQLIntegrityConstraintViolationException e) 
				{
					System.out.println("duplicated family");
				}
				
				Genus genus = new Genus();
				genus.setFamily(family);
				genus.setName(specieElement.text().split(" ")[0]);
				genus.setDescription(specieElement.text().split(" ")[0]);
				
				try
				{
					cGenus.create(genus);
				}
				catch (SQLIntegrityConstraintViolationException e) 
				{
					System.out.println("duplicated genus");
				}
				
				
				Specie specie = new Specie();
				specie.setScientificName(specieElement.text().split(" ")[0]+" "+specieElement.text().split(" ")[1]);
				String usualName = data.get(5).text()==null?"empty":(data.get(5).text().length()>50?data.get(5).text().substring(0,50):data.get(5).text());
				specie.setUsualName(usualName.replaceAll("'", ""));
				String obs = data.get(13).text()==null?"empty":(data.get(13).text().length()>500?data.get(13).text().substring(0,500):data.get(13).text());
				specie.setObservations(obs.replaceAll("'", ""));
				specie.setBiologist(biolgist);
				specie.setGenus(genus);
				
				try{
					cSpecie.create(specie);
				}
				catch (SQLIntegrityConstraintViolationException e) 
				{
					System.out.println("duplicated specie");
				}
				
				
				System.out.println(specie.getScientificName() +" - "+specie.getUsualName());
			}
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
